﻿using System;
using System.Collections.Generic;
using System.Net.Mail;
using System.Text;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using SelArom.Net.Sitefinity.API.News;
using Telerik.Cms.Engine;
using Telerik.Cms.Web.UI;
using Telerik.News;
using Telerik.Workflow;
using Telerik.Workflow.WebControls;
using Telerik;
using Telerik.Cms.Engine.Security;

namespace SelArom.Net.Sitefinity.Templates
{
	/// <summary>
	/// UserControl base for the NewsItemPreview external template to send notification emails for content status change
	/// </summary>
	public class NewsItemPreview : ContentItemPreviewBase
	{
		NewsManager newsMgr;
		protected override void OnLoad(EventArgs e)
		{
			// initialize generic manager and local events manager
			DefaultProvider = NewsManager.DefaultContentProvider;
			mgr = new ContentManager(DefaultProvider);
			
			// copy of manager for provider meta keys since NewsManager does not inherit from ContentManager
			newsMgr = new NewsManager(DefaultProvider);

			// initialize everything else (including content item data)
			base.OnLoad(e);

			// add custom handler for publishing
			if (wfButton == null) return;
			wfButton.Command += Clear_Cache;
		}

		protected override string ForApprovalMessage()
		{
			var sb = new StringBuilder();
			// create email body
			sb.AppendFormat("{0} has submitted an news item for approval!\n\nhttp://{1}/Sitefinity/Admin/Modules.aspx?module=News\n\n", usr.UserName, Request.Url.Host);
			sb.Append("\nPlease REVIEW the entry and correct any issues before approving for publication!");

			// add event details if found
			if (contentItem != null)
			{
				sb.Append("News Item Details:\n\n");

				// ensure a title
				var title = contentItem.GetMetaData("Title");
				if (title == null) title = contentItem.GetMetaData("Name");
				if (title == null) title = "N/A";
				
				sb.AppendFormat("Title: {0}\nPublication Date: {1:MMM d, yyyy hh:mm tt}\nExpiration Date: {2:MMM d, yyyy hh:mm tt}", title, contentItem.GetMetaData(newsMgr.PublicationDateField), contentItem.GetMetaData(newsMgr.ExpirationDateField));
			}
			return sb.ToString();
		}

		protected override string ApprovedMessage()
		{
			var sb = new StringBuilder();
			// create email body
			sb.AppendFormat("{0} has approved an news item for publication!\n\nhttp://{1}/Sitefinity/Admin/Modules.aspx?module=News\n\n", usr.UserName, Request.Url.Host);

			// add event details if found
			if (contentItem != null)
			{
				sb.Append("News Item Details:\n\n");

				// ensure a title
				var title = contentItem.GetMetaData("Title");
				if (title == null) title = contentItem.GetMetaData("Name");
				if (title == null) title = "N/A";

				sb.AppendFormat("Title: {0}\nPublication Date: {1:MMM d, yyyy hh:mm tt}\nExpiration Date: {2:MMM d, yyyy hh:mm tt}", title, contentItem.GetMetaData(newsMgr.PublicationDateField), contentItem.GetMetaData(newsMgr.ExpirationDateField));
			}
			return sb.ToString();
		}

		protected void Clear_Cache(object sender, CommandEventArgs e)
		{
			// make sure the command is already parsed
			if (string.IsNullOrEmpty(wfCommand)) return;

			// make sure content item was found
			if (contentItem == null) return;

			// clear cache on publish
			if (wfCommand.ToLower() == "publish")
				NewsAPI.Clear();
		}
	}
}